package com.android.calldelay;

import com.android.calldelay.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;

public class WhatIsActivity extends Activity implements
android.view.View.OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_page_3);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

		Typeface typeface = Typeface.createFromAsset(getAssets(),
				"font/arial.ttf");
		((Button) findViewById(R.id.button_whatis_setup))
		.setOnClickListener(this);


		((Button) findViewById(R.id.button_whatis_home))
		.setOnClickListener(this);
		Button btnVisitOurSite = (Button) findViewById(R.id.btn_visit_our_site);
		btnVisitOurSite.setTypeface(typeface);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button_whatis_setup:
			moveToSetup(v);
			break;

		case R.id.button_whatis_home:

			/*Intent intent = new Intent(v.getContext(), MainActivity.class);
			startActivity(intent );*/
			finish();


			break;

		default:
			break;
		}
	}

	private void moveToSetup(View v) {
		// TODO Auto-generated method stub

		SharedPreferences setting = v.getContext().getSharedPreferences(
				AppInfo.APP_PREF, 0);

		boolean isPassEnable = setting.getBoolean(AppInfo.KEY_PASS_CHECKBOX,
				false);
		if (isPassEnable) {
			showPasswordDialog(v, setting);
		} else {

			Intent intent = new Intent(v.getContext(), SetUpActivity.class);
			startActivity(intent);
			finish();
		}

	}

	private void showPasswordDialog(View v, final SharedPreferences setting) {
		// TODO Auto-generated method stub
		DisplayMetrics displayMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

		LayoutInflater inflater = (LayoutInflater) v.getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layoutDialog = inflater.inflate(R.layout.layout_stop_dialog, null);
		final EditText etPassword = (EditText) layoutDialog
				.findViewById(R.id.editText_password);
		Button btnOk = (Button) layoutDialog.findViewById(R.id.btn_ok);
		Button btnCancel = (Button) layoutDialog.findViewById(R.id.btn_cancel);

		int displayHeight = displayMetrics.heightPixels;
		int displayWidth = displayMetrics.widthPixels;
		final Dialog dialog = new Dialog(v.getContext());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(layoutDialog, new LayoutParams(displayWidth - 20,
				displayHeight / 3));
		dialog.getWindow().setBackgroundDrawable(new ColorDrawable());
		dialog.show();
		btnOk.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String password = etPassword.getText().toString();
				String savedPassword = setting.getString(AppInfo.KEY_PASSWORD,
						"");
				Animation shake = null;
				if (password.equals(savedPassword)) {

					Intent intent = new Intent(v.getContext(),
							SetUpActivity.class);
					startActivity(intent);
					finish();

				} else {
					shake = AnimationUtils.loadAnimation(v.getContext(),
							R.anim.shake);
					etPassword.startAnimation(shake);
					etPassword.setError("Invalid password");
				}
			}
		});

		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

	}

}
